package week2;
import java.util.Arrays;
import java.util.Scanner;

public class Idjiance 
{
	public static void main(String[] args)
	{
		String[] IdDigits = new String[100];
		String[] bir = new String[100];
		int[] birthDigits = new int[100];
		Scanner in=new Scanner(System.in);
		
			int n=Integer.parseInt(in.nextLine());
			for(int i=0;i<n;i++)
			{
				IdDigits[i]=in.nextLine();
			}
			while(true) {
			String choice=in.nextLine();
			switch(choice)
			{
			case "sort1":
				birthDigits=sortbirth1(IdDigits,n);
				for(int i=0;i<n;i++)
				{
					bir[i] = String.valueOf(birthDigits[i]);
				}
				for(int i=0;i<n;i++)
				{
					System.out.println(getBirthDate(bir[i]));
				}
				break;
			case "sort2":
				sortbirth(IdDigits,n);
				break;
			default:
				System.out.println("exit");
			}
		}
	}
	private static String getBirthDate(String id) {
		return(id.substring(0,4)+"-"+id.substring(4,6)+"-"+id.substring(6,8));
		
	}
	private static int[] sortbirth1(String[] digits,int n)
	{
		String[] birth = new String[100];
		int[] birthday=new int[100];
		for(int i=0;i<n;i++)
		{
			birth[i]=digits[i].substring(6,14);
		}
		birthday=convertToIntDigits(birth,n);
		Arrays.sort(birthday);
		return birthday;
	}
	private static void sortbirth(String[] digits,int n)
	{
		String[] birth = new String[100];
		String[] strbirthday = new String[100];
		int[] birthday=new int[100];
		for(int i=0;i<n;i++)
		{
			birth[i]=digits[i].substring(6,14);
		}
		birthday=convertToIntDigits(birth,n);
		Arrays.sort(birthday);
		strbirthday=convertToStringDigits(birthday,n);
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
		{
			if(strbirthday[i].equals(digits[j].substring(6, 14))) {
				System.out.println(digits[j]);break;}
		}
	}
	private static int[] convertToIntDigits(String[] strDigits,int n) {
		int[] digits=new int[n];
		for(int i=0;i<digits.length;i++)
		{
			digits[i]=Integer.parseInt(strDigits[i]);
		}
		return digits;
	
}
private static String[] convertToStringDigits(int[] strDigits,int n) {
	String[] digits=new String[n];
	for(int i=0;i<n;i++)
	{
		digits[i]=String.valueOf(strDigits[i]);
	}
	return digits;
}
}
